package com.example.product_practices.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.product_practices.domain.Groups;
import com.example.product_practices.domain.User;
import com.example.product_practices.service.IGroupsService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import xin.altitude.cms.common.entity.AjaxResult;
import xin.altitude.cms.common.entity.PageEntity;

import java.util.Arrays;

@RestController
@CrossOrigin
@RequestMapping("/groups")
@Api(tags = "群组表")
public class GroupsController {
    @Autowired
    private IGroupsService groupsService;

    @GetMapping("/page")
    public AjaxResult page(PageEntity pageEntity, Groups groups) {
        return AjaxResult.success(groupsService.page(pageEntity.toPage(), Wrappers.lambdaQuery(groups)));
    }

    @GetMapping("/list")
    public AjaxResult list(Groups groups) {
        return AjaxResult.success(groupsService.list(Wrappers.lambdaQuery(groups)));
    }

    @PostMapping("/add")
    public AjaxResult add(@RequestBody Groups groups) {
        return AjaxResult.success(groupsService.save(groups));
    }

    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody Groups groups) {
        return AjaxResult.success(groupsService.updateById(groups));
    }
    @PostMapping("/addChange")
    public AjaxResult addChange(@RequestBody Groups groups){
        groupsService.save(groups);
        return AjaxResult.success(groupsService.maxGroupID());
    }

    @DeleteMapping("/delete/{ids}")
    public AjaxResult delete(@PathVariable Integer[] ids) {
        return AjaxResult.success(groupsService.removeByIds(Arrays.asList(ids)));
    }

    @GetMapping(value = "/detail/{id}")
    public AjaxResult detail(@PathVariable("id") Integer id) {
        return AjaxResult.success(groupsService.getById(id));
    }


    /**
     * 某用户下的群组模糊查询
     * @param pageEntity
     * @param partName
     * @param userId
     * @return
     */
    @GetMapping("/groupsPageLike")
    public AjaxResult groupsPagesLike(PageEntity pageEntity,String partName,int userId){
        QueryWrapper<Groups> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        queryWrapper.like("name",partName);
        queryWrapper.eq("used",1);
        Page<Groups> page = groupsService.page(pageEntity.toPage(), queryWrapper);

        return AjaxResult.success(page);

    }

    @DeleteMapping("/logicDeleteGroups/{ids}")
    public AjaxResult logicDeleteGroups(@PathVariable Integer[] ids){

        for (Integer id : ids) {
            UpdateWrapper<Groups> wrappers = new UpdateWrapper<>();
            wrappers.eq("id",id);
            wrappers.set("used",0);
            groupsService.update(wrappers);
        }
        return AjaxResult.success(ids.length);
    }

}
